#ifndef ZHY_MUDUO_BASE_CONDITION_H
#define ZHY_MUDUO_BASE_CONDITION_H

#include "Mutex.h"
#include <pthread.h>
namespace _zhy_muduo{
class Condition
{
public:
	explicit Condition(MutexLock &mutex)
		: mutex_(mutex)
	{
		MCHECK(pthread_cond_init(&pcond_, NULL));
	}
	~Condition()
	{
		MCHECK(pthread_cond_destroy(&pcond_));
	}

	void wait()
	{
		MutexLock::UnassignGuard ug(mutex_);
		MCHECK(pthread_cond_wait(&pcond_, mutex_.getPthreadMutex()));
	}

	//returns ture if time out, false otherwise.
	bool waitForSeconds(double seconds);

	void notify()
	{
		MCHECK(pthread_cond_signal(&pcond_));
	}

	void notifyAll()
	{
		MCHECK(pthread_cond_broadcast(&pcond_));
	}

private:
	MutexLock &mutex_;
	pthread_cond_t pcond_;
	DISALLOW_COPY_AND_ASSIGN_11(Condition);
};
}//namespace _zhy_muduo

#endif //ZHY_MUDUO_BASE_CONDITION_H